package com.cian.service.impl;

import com.cian.entity.Stock;
import com.cian.entity.dto.ClientCommodityDTO;
import com.cian.entity.dto.StockDTO;
import com.cian.mapper.StockMapper;
import com.cian.service.IStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 子终端对应的商品库存在mysql的持久表 服务实现类
 * </p>
 *
 * @author Cian
 * @since 2021-03-09
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {
    @Autowired
    StockMapper stockMapper;

    @Override
    public List<Integer> getSubsystemStockForCommodity(String serverCode) {
        return stockMapper.getSubsystemStockForCommodity(serverCode);
    }

    @Override
    public void selectStockByList(String serverCode, Integer[] add) {
        stockMapper.selectStockByList(serverCode, add);
    }



    @Override
    public void deleteStockByList(String serverCode, Integer[] delete) {
        try{
            // 关闭安全模式
            stockMapper.setSalfValue(0);
            stockMapper.deleteStockByList(serverCode, delete);
            // 开启安全模式
            stockMapper.setSalfValue(1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<StockDTO> getStockByCode(String serverCode, int nextPage, int pageMaxNum) {
        List<StockDTO> list = stockMapper.getStockByCode(serverCode, nextPage, pageMaxNum);
        // 判断list是否为空，如果为空，则返回null
        if (list.isEmpty()) return null;
        return list;
    }

    @Override
    public StockDTO getStockInform(String serverCode, int coId) {
        return stockMapper.getStockInform(serverCode, coId, 1);
    }

    @Override
    public boolean buyingSetting(String serverCode, int coId, int amount) {
        try{
            // 关闭安全模式
            stockMapper.setSalfValue(0);
            stockMapper.changeStock(serverCode, coId, amount);
            // 开启安全模式
            stockMapper.setSalfValue(1);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<StockDTO> getStockByCodeForAll(String serverCode, int pageMaxNum) {
        List<StockDTO> list = stockMapper.getStockByCodeForAll(serverCode, pageMaxNum);
        // 判断list是否为空，如果为空，则返回null
        if (list.isEmpty()) return null;
        return list;
    }

    @Override
    public List<ClientCommodityDTO> getClientCommodityList(String serverCode) {
        List<ClientCommodityDTO> list = stockMapper.getClientStockByCode(serverCode);
        // 判断list是否为空，如果为空，则返回null
        if (list.isEmpty()) return null;
        return list;
    }

    @Override
    public void deleteStockByCommodityCode(int coId) {
        stockMapper.deleteStockByCommodityCode(coId);
    }

    @Override
    public boolean updateStockByCode(String serverCode, int coId, double price, int stock) {
        try{
            Map<String, Object> map = new HashMap<>();
            map.put("s_code", serverCode);
            map.put("co_id", coId);
            Stock temp = stockMapper.selectByMap(map).get(0);
            temp.setPrice(price);
            int num = temp.getAmount() + stock >= 0 ? temp.getAmount() + stock : 0;
            temp.setAmount(num);
            stockMapper.updateById(temp);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


}
